﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using ProtoBuf;

public class ProtobufTool
{
    public static byte[] Serialize<T>(T msg)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            Serializer.Serialize<T>(ms, msg);
            byte[] data = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(data, 0, data.Length);
            return data;
        }
    }

    public static T Deserialize<T>(byte[] msgBytes)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            ms.Write(msgBytes, 0, msgBytes.Length);
            ms.Position = 0;
            //gameMsg为返回结果
            return Serializer.Deserialize<T>(ms);
        }
    }
}

[ProtoContract]
public class SimpleGameMsg
{
    [ProtoMember(1)]
    public int msgType;
    [ProtoMember(2)]
    public string msgContent;
}
